package com.epoint.mobileoa.ui.wsgw;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.XMLUtil;
import com.epoint.mobileoa.ui.wsgw.bizlogic.AnalysisForm;
import com.epoint.mobileoa.ui.wsgw.bizlogic.AttachDeal;
import com.epoint.mobileoa.ui.wsgw.bizlogic.StringDeal;
import com.epoint.mobileoa.ui.wsgw.model.AttFileInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.FlowInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.FormInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.Handle_GetUnHandleList_Model;
import com.epoint.mobileoa.ui.wsgw.model.NextStepInfoModel;
import com.epoint.mobileoa.ui.wsgw.task.GetAttachTask;
import com.epoint.mobileoa.ui.wsgw.task.GetDbDetailTask;

/**
 * @author liyc
 * @time 2012-12-17 下午3:39:18
 * @annotation 
 */
public class WsDbDetailView extends SuperView {
	private WebView wv,flow_wv;
	private Handle_GetUnHandleList_Model h;
	private TextView tv_chulibuzhou,tv_tijiaoren;
	private Button btBottomFormInfo,btBottomFlowInfo,btAttachInfo;
	private ListView lvAttach;
	private List<AttFileInfoModel> attFileInfoList;
	private LinearLayout ll_operation;

	@Override
	public void initControls() {
		ll_operation = (LinearLayout) findViewById(R.id.ll_operation);
		lvAttach = (ListView) findViewById(R.id.lvAttach);
		btAttachInfo = (Button) findViewById(R.id.btAttachInfo);
		btBottomFormInfo = (Button) findViewById(R.id.btBottomFormInfo);
		btBottomFlowInfo = (Button) findViewById(R.id.btBottomFlowInfo);
		btAttachInfo.setOnClickListener(this);
		btBottomFlowInfo.setOnClickListener(this);
		btBottomFormInfo.setOnClickListener(this);
		tv_chulibuzhou = (TextView) findViewById(R.id.tv_chulibuzhou);
		tv_tijiaoren = (TextView) findViewById(R.id.tv_tijiaoren);
		wv = (WebView) findViewById(R.id.wv);
		wv.setBackgroundColor(0);
		flow_wv = (WebView) findViewById(R.id.flow_wv);
		flow_wv.setBackgroundColor(0);
		h = (Handle_GetUnHandleList_Model) getIntent().getSerializableExtra("model");
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("model", h);
		new GetDbDetailTask(this,1, param, null,2);
		lvAttach.setOnItemClickListener(this);

	}

	@Override
	public void refresh(Object... param) throws Exception {
	}

	@Override
	public void refreshForNewTask(int TaskId, Object... param){
		if(TaskId==1){
			String bs = param[0].toString();
			String ReturnInfo = StringHelp.getXMLAtt(bs, "ReturnInfo");
			String Status = StringHelp.getXMLAtt(ReturnInfo, "Status");
			if(Status.equals("True"))
			{
				//处理步骤
				tv_chulibuzhou.setText(Html
						.fromHtml("<font color=red>当前步骤:</font><font color=black>"
								+ StringDeal.getBZ(h.Title) + "</font>"));
				tv_tijiaoren.setText(Html
						.fromHtml("<font color=red>提交人:</font><font color=black>"
								+ h.FromDispName + "</font>"));

				//表单信息
				String forminfo = StringHelp.getXMLAttOut(bs, "FormInfo");
				List<FormInfoModel> forminfolist = null;
				try {
					forminfolist = AnalysisForm.getFormInfoList(forminfo);
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

				StringBuilder sbhtml = new StringBuilder();
				for(FormInfoModel f:forminfolist)
				{
					sbhtml.append(StringHelp.getFormInfoHtml(this, f.FieldChsName, f.AttributeTextValue));
				}
				wv.loadDataWithBaseURL(null, sbhtml.toString(),"text/html","utf-8",null);

				//处理过程
				String FlowInfoLstXML = StringHelp.getXMLAttOut(bs, "FlowInfoLst");
				List<FlowInfoModel> flowInfoList = XMLUtil.DomAnalysisCommon(FlowInfoLstXML, FlowInfoModel.class);
				flow_wv.loadDataWithBaseURL(null,
						StringDeal.pingProcessXML_WS(flowInfoList), "text/html", "utf-8",
						null);

				//附件
				String AttFileInfoLstXML = StringHelp.getXMLAttOut(bs, "AttFileInfoLst");
				attFileInfoList = XMLUtil.DomAnalysisCommon(AttFileInfoLstXML, AttFileInfoModel.class);
				AttachListAdatper adapter = new AttachListAdatper();
				lvAttach.setAdapter(adapter);

				
				if(h.CanHanle.equals("0"))
				{
					ll_operation.setVisibility(View.GONE);
					TextView tv = (TextView) findViewById(R.id.tvnexttag);
					tv.setText("请在PC端操作！");
					tv.setTextColor(getResources().getColor(R.color.red));
					tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
				}
				else{
					//按钮
					String NextStepInfoLstXML = StringHelp.getXMLAttOut(bs, "NextStepList");
					List<NextStepInfoModel> NextStepInfoList = null;
					try {
						NextStepInfoList = AnalysisForm.getNextStepInfoList(NextStepInfoLstXML);
					} catch (XmlPullParserException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					//ToastHelp.showAndroidToastWorning(this, NextStepInfoList.size()+"");
					for(int i=0;i<NextStepInfoList.size();i++)
					{
						final NextStepInfoModel nm = NextStepInfoList.get(i);
						final int position = i;
						final Button btOperation = new Button(this);
						btOperation.setText(nm.StepName);
						btOperation.setTag(i);
						if (getYGColor().equals("yellow")) {
							btOperation.setTextColor(Color.BLACK);
							btOperation.setBackgroundResource(R.drawable.btn_white_selectorl);
						} else {
							btOperation.setTextColor(Color.WHITE);
							btOperation
							.setBackgroundResource(R.drawable.btn_blue_selector);
						}
						btOperation.setTextSize(18);

						btOperation.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {

								//							if(nm.CanEditUser.equals("0")){
								//								ToastHelp.showAndroidToastWorning(getContext(), "直接提交");
								//								return;
								//							}

								Intent nextstepintent = new Intent(getContext(),WsNextStepSignView.class);
								nextstepintent.putExtra("nextstepmodel", nm);
								nextstepintent.putExtra("Handle_GetUnHandleList_Model",h);
								nextstepintent.putExtra("viewtitle", "事项处理");
								nextstepintent.putExtra("currentStep", StringDeal.getBZ(h.Title));
								startActivity(nextstepintent);
							}
						});
						ll_operation.addView(btOperation);

					}
				}
			}
			else{
				ToastHelp.showAndroidToastWorning(this, StringHelp.getXMLAtt(bs, "Description"));
			}
		}
		else if(TaskId==2)
		{
			String bs = param[0].toString();
			String ReturnInfo = StringHelp.getXMLAtt(bs, "ReturnInfo");
			String Status = StringHelp.getXMLAtt(ReturnInfo, "Status");
			if(Status.equals("True"))
			{
				String isBigFile = StringHelp.getXMLAtt(bs, "isBigFile");
				if(isBigFile.equals("1"))
				{
					ToastHelp.showAndroidToastWorning(this, "无法在移动设备上查看大附件");
					return;
				}

				String AttFileName = StringHelp.getXMLAtt(bs, "AttFileName");
				String AttContent = StringHelp.getXMLAtt(bs, "AttContent");
				AttachDeal.saveAndOpenFile(getContext(), AttContent, h.MessageItemGuid, AttFileName);
			}
			else{
				ToastHelp.showAndroidToastWorning(this, StringHelp.getXMLAtt(bs, "Description"));
			}
		}
	}

	@Override
	public void onClick(View v) {
		if(btBottomFlowInfo==v)
		{
			flow_wv.setVisibility(View.VISIBLE);
			wv.setVisibility(View.GONE);
			lvAttach.setVisibility(View.GONE);
		}
		else if(btBottomFormInfo==v){
			flow_wv.setVisibility(View.GONE);
			wv.setVisibility(View.VISIBLE);
			lvAttach.setVisibility(View.GONE);
		}
		else if(btAttachInfo==v)
		{
			lvAttach.setVisibility(View.VISIBLE);
			flow_wv.setVisibility(View.GONE);
			wv.setVisibility(View.GONE);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		AttFileInfoModel a = attFileInfoList.get(position);
		if(AttachDeal.openFile(this, h.MessageItemGuid, a.AttFileName))
		{
			return;
		}
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("model", attFileInfoList.get(position));
		new GetAttachTask(this,2, param, null, 2);
	}

	@Override
	public int getLayoutInt() {
		return R.layout.wsdbdetailview;
	}

	class AttachListAdatper extends BaseAdapter{

		@Override
		public int getCount() {
			return attFileInfoList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());
				convertView = mInflater.inflate(R.layout.ws_attachlayout, null);
				holder.ivIcon = (ImageView) convertView
						.findViewById(R.id.ivIcon);
				holder.tvFileName = (TextView) convertView
						.findViewById(R.id.tvFileName);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			AttFileInfoModel a = attFileInfoList.get(position);
			int imgRes = R.drawable.fc_file;
			if(a.AttFileName.endsWith(".doc")||a.AttFileName.endsWith(".docx"))
			{
				imgRes = R.drawable.img_attach_doc;
			}
			else if(a.AttFileName.endsWith(".pdf"))
			{
				imgRes = R.drawable.img_pdf_48;
			}
			else if(a.AttFileName.endsWith(".xls")||a.AttFileName.endsWith(".xlsx"))
			{
				imgRes = R.drawable.img_attach_excel;
			}

			setTextViewFontColor(holder.tvFileName);
			holder.ivIcon.setBackgroundResource(imgRes);
			holder.tvFileName.setText(a.AttFileName);
			return convertView;
		}

		public class ViewHolder {
			public ImageView ivIcon;
			public TextView tvFileName;
		}
	}
}